// pages/user/mycomment.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: false,
    userInfo: {},
    order_id: 0,
    isComment: 0,
    goodsList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      order_id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    //用户授权登录
    app.login(function (userInfo) {
      that.setData({
        userInfo: userInfo
      });
      that.setGoodsData();
    });
  },

  //初始化商品详情
  setGoodsData: function () {
    var that = this;
    var paraArr = new Array();
    paraArr['user_id'] = that.data.userInfo.userID;
    paraArr['id'] = that.data.order_id;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api_comment/' + paraArr['id'],
      data: { user_id: paraArr['user_id'] , sign: sign },
      method: 'GET',
      header: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          goodsList: res.data.goods,
          isComment: res.data.is_comment,
          hiddenLoading: true
        });
      }
    })
  },
  /**
   * 提交评论
   */
  formSubmit: function (e) {
    var that = this;
    var info = e.detail.value;
    var info_str = '';
    for (var key in info) {
      info_str = info_str + info[key] + "|"
　　}
    //拼接其他信息
    var order_goods_id = '';
    var m_rate = '';
    var goods = that.data.goodsList;
    for (var key in goods) {
      order_goods_id = order_goods_id + goods[key]['id'] + '|';
      m_rate = m_rate + '5' + '|';
    }
    //添加评论
    wx.request({
      url: rootDocment + '/api_comment',
      data: { order_id: that.data.order_id, order_goods_id: order_goods_id, m_rate: m_rate, m_info: info_str, user_id: that.data.userInfo.userID },
      method: 'POST',
      header: {},
      success: function (res) {
        wx.navigateBack({
          delta: 1
        })
      }
    })

  }

})